package com.xmut.mall.store.service.impl;

import com.xmut.mall.store.searchparam.LogisticsSearchParam;
import com.xmut.mall.store.utils.PageUtil;
import com.xmut.mall.store.utils.RespBean;
import com.xmut.mall.store.vo.PageVO;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmut.mall.store.utils.PageUtils;
import com.xmut.mall.store.utils.Query;

import com.xmut.mall.store.dao.LogisticsDao;
import com.xmut.mall.store.entity.LogisticsEntity;
import com.xmut.mall.store.service.LogisticsService;


@Service("logisticsService")
public class LogisticsServiceImpl extends ServiceImpl<LogisticsDao, LogisticsEntity> implements LogisticsService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<LogisticsEntity> page = this.page(
                new Query<LogisticsEntity>().getPage(params),
                new QueryWrapper<LogisticsEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取商家物流公司列表，如果已选择则checked有值
     * @param storeId
     * @return
     */
    @Override
    public RespBean getLogistics(Long storeId) {
        List<LogisticsEntity> list = this.baseMapper.getLogistics(storeId);

        return RespBean.success("获取成功",list);
    }

    /**
     * 官方管理平台获取物流公司分页数据
     * @param vo
     * @param param
     * @return
     */
    @Override
    public RespBean getManagerLogisticsByPage(PageVO vo, LogisticsSearchParam param) {
        IPage<LogisticsEntity> page = this.baseMapper.getManagerLogisticsByPage(PageUtil.initPage(vo),param);

        return RespBean.success("获取成功",page);
    }

    /**
     * 添加物流公司
     * @param entity
     * @return
     */
    @Override
    public RespBean addLogistics(LogisticsEntity entity) {
        entity.setCreateTime(new Date());
        boolean save = this.save(entity);
        if (save){
            return RespBean.success("添加成功",entity);
        }else {
            return RespBean.error("添加失败",entity);
        }

    }

    /**
     * 根据ID删除物流公司信息
     * @param id
     * @return
     */
    @Override
    public RespBean deleteLogistics(Long id) {
        boolean b = this.removeById(id);
        if (b){
            return RespBean.success("删除成功");
        }else {
            return RespBean.error("删除失败");
        }

    }

    /**
     * 修改物流公司信息
     * @param entity
     * @return
     */
    @Override
    public RespBean updateLogistics(LogisticsEntity entity) {
        boolean b = this.updateById(entity);
        if (b){
            return RespBean.success("修改成功");
        }else {
            return RespBean.error("修改失败");
        }

    }

    /**
     * 获取所有的物流公司
     * @return
     */
    @Override
    public RespBean getAllLogistics() {
        return RespBean.success("获取成功",this.list());
    }

    /**
     * 获取商家已选择物流公司列表
     * @param storeId
     * @return
     */
    @Override
    public RespBean getChecked(Long storeId) {
        List<LogisticsEntity> list = this.baseMapper.getChecked(storeId);

        return RespBean.success("获取成功",list);

    }


}